<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>微信登录二维码</title>
  <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
  <script src="../../static/weCatJs/qrcode.js" th:src="@{/weCatJs/qrcode.js}"></script>
  <script th:src="@{/js/jquery.cookie.js}"></script>
</head>

<script type="text/javascript">

  /*  $(function(){
      　　var div1 = document.getElementById("div1");
      　　alert(div1);
   }) */
  var c = null;
  //监听
  function getInfoJson(){
    $.ajax({
      url: "/getInfoJson",
      type: "get",
      success: function (data) {
        if(data != "no"){
          $("#userinfo").html(data);
          //登录成功，取消监听
          clearInterval(c);
          var jsonData  = eval("("+data+")");
          var millisecond = new Date().getTime();
          var expiresTime = new Date(millisecond + 60 * 1000 * 1);

            $.cookie('nickName', jsonData.nickName, {
            expires: expiresTime,
          });
          $.ajax({
            url: "/user/wechatLogin",
            type: "post",//请求类型
            //data: JSON.stringify(obj.field),
            //contentType:  "application/json; charset=utf-8",
            dataType: "json",//服务端返回给浏览器的数据类型
            success: function (result) {//执行成功的回调函数,次data非彼data，是服务器端返回的json的一个变量
              if (result.code == 200) {
               /* layui.data(setter.tableName, {
                  key: setter.request.tokenName,
                  value: result.sessionId
                });*/
                location.replace('index');
                //location.href = 'index';
                layer.msg('登入成功', {
                  offset: '15px',
                  icon: 1,
                  time: 1000
                }, function() {

                  location.href = 'index'; //后台主页
                  // location.href="javascript:history.go(-1)"
                });
              } else {
                layer.msg(result.msg, {
                  icon: 5
                });
              }
            }
          });
        }
      }
    });
  }


  function wechatLogin(){
    $.ajax({
      url: "/wxLoginPage",
      type: "POST",
      success: function (data) {
        $("#sessionId").val(data.sessionId);
        //生成二维码
        var qrcode = new QRCode(document.getElementById("code"), {
          width : 200,
          height : 200
        });
        qrcode.makeCode(data.uri);
        //监听是否成功登录(每秒执行一次 getInfoJson方法)
        c  = setInterval(getInfoJson,1000);
      }
    });
  }
</script>
<body>
<!--<input type="button" value="微信登录" th:onclick="wechatLogin()" />-->
<input type="hidden" id="sessionId"/>
<br/><br/><br/><br/>
<div id="code"></div>
<!--<div id="userinfo"></div-->
</body>
<script type="text/javascript">
  $(function() {
    wechatLogin()
  })
</script>
</html>
